package com.zxbit.flash.ui.activity.login;

import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.zxbit.flash.R;
import com.zxbit.flash.cache.Acache;
import com.zxbit.flash.cache.AcacheKeys;
import com.zxbit.flash.base.BaseTitleActivity;
import com.zxbit.flash.common.Constant;
import com.zxbit.flash.common.RouterMap;
import com.zxbit.flash.mvpview.LoginMvpView;
import com.zxbit.flash.presenter.LoginPresenter;
import com.zxbit.flash.response.FaceBookLoginResponse;
import com.zxbit.flash.response.LoginResponse;
import com.zxbit.flash.rxbus.EventMsg;
import com.zxbit.flash.rxbus.Events;
import com.zxbit.flash.rxbus.RxBus;
import com.zxbit.flash.utils.StringUtils;
import com.zxbit.flash.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

@Route(name = "登录页", path = RouterMap.LOGIN_PAGE)
public class LoginActivity extends BaseTitleActivity implements LoginMvpView {

    @BindView(R.id.user_name_text)
    EditText editUserName;
    @BindView(R.id.password_text)
    EditText editPassWord;
    @Autowired(name = Constant.USER_NAME)
    String userName; // 用户名
    private LoginPresenter loginPresenter;

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        editPassWord.setTransformationMethod(PasswordTransformationMethod.getInstance());
    }

    @Override
    protected void onUIReady() {
        loginPresenter = new LoginPresenter(this);

        if (!StringUtils.isEmpty(userName)) {
            editUserName.setText(userName);
        }
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_login;
    }

    @OnClick({R.id.btn_login, R.id.btn_register, R.id.btn_forget_psw})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                String hintStr = checkData();
                if (StringUtils.isEmpty(hintStr)) {
                    loginPresenter.login(editUserName.getText().toString().trim(), editPassWord.getText().toString().trim());
                    showLoadingView();
                } else {
                    ToastUtils.showShort(this, hintStr);
                }
                break;
            case R.id.btn_register:
                ARouter.getInstance().build(RouterMap.REGISTER_PAGE).navigation();
                break;
            case R.id.btn_forget_psw:
                ARouter.getInstance().build(RouterMap.FIND_PASSWORD_PAGE).navigation();
                break;
        }
    }

    @OnCheckedChanged(R.id.show_psw_text)
    public void OnCheckedChanged(boolean isChecked) {
        if (isChecked) {
            editPassWord.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            editPassWord.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
    }

    // 登陆成功
    private void handleLoginSuc(LoginResponse loginResponse) {
        String token = loginResponse.getData().getToken();

        if (StringUtils.isEmpty(token)) {
            ToastUtils.showShort(LoginActivity.this, getStringResource(R.string.token_is_null));
            return;
        }

        Acache.get(this).put(AcacheKeys.TOKEN, loginResponse.getData().getToken());
        EventMsg msg = new EventMsg();
        msg.setMsg(Events.LOGIN_SUCCESS);
        RxBus.getInstance().post(msg);
        ToastUtils.showShort(LoginActivity.this, getStringResource(R.string.login_page_login_success));
        ARouter.getInstance().build(RouterMap.MAIN_ACTIVITY_PAGE).navigation();
    }

    // 检查数据
    private String checkData() {
        if (editUserName != null && editPassWord != null) {
            String uerName = editUserName.getText().toString();
            String passwordStr = editPassWord.getText().toString();
            if (StringUtils.isEmpty(uerName)) {
                return getStringResource(R.string.login_type_user_name);
            }
            if (StringUtils.isEmpty(passwordStr)) {
                return getStringResource(R.string.login_type_pwd);
            }
        }
        return null;
    }

    @Override
    public void onGetDataNext(LoginResponse loginResponse) {
        dismissLoadingView();
        if (loginResponse.getStatus() == Constant.HTTP_OK && loginResponse.hasData()) {
            handleLoginSuc(loginResponse);
        } else {
            ToastUtils.showShort(LoginActivity.this, loginResponse.getMessage());
        }
    }

    @Override
    public void onGetDataNext(FaceBookLoginResponse response) {}

    @Override
    public void onGetDataError(Throwable throwable) {
        dismissLoadingView();
    }
}
